package com.xcf.controller.business.common;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReadStatus;
import org.jxls.reader.XLSReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xcf.controller.base.BaseController;
import com.xcf.controller.base.ExportDataHandler;
import com.xcf.entity.Page;
import com.xcf.service.business.common.paymentunit.PaymentUnitService;
import com.xcf.util.Const;
import com.xcf.util.DateUtil;
import com.xcf.util.FileUpload;
import com.xcf.util.ObjectExcelRead;
import com.xcf.util.PageData;
import com.xcf.util.PathUtil;
import com.xcf.util.ResponseResult;

import cn.com.xcf.dao.model.FactBookingNote;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/paymentUnit")
public class PaymentUnitController extends BaseController implements ExportDataHandler{
	@Resource
	private PaymentUnitService paymentUnitService;
	@RequestMapping(value="/list")
	public ModelAndView paymentUnitList()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/common/paymentunit/payment_unit_list");
		return view;
		
	}
	/**
	 * 查询支付单位
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/findPaymentUnitInfo")
	@ResponseBody
	public Object findPaymentUnitInfo(int page, int limit)throws Exception{
			PageData pd = new PageData();
			pd = this.getPageData();
			PageHelper.startPage(page, limit);	
			if(pd.getString("SUPPLIER")!=null && pd.getString("SUPPLIER")!=""){
				String SUPPLIER= URLDecoder.decode(pd.getString("SUPPLIER"), "utf-8");
				pd.put("SUPPLIER", SUPPLIER);
			}
			if(pd.getString("ACCOUNT_BANK")!=null && pd.getString("ACCOUNT_BANK")!=""){
				String ACCOUNT_BANK= URLDecoder.decode(pd.getString("ACCOUNT_BANK"), "utf-8");
				pd.put("ACCOUNT_BANK", ACCOUNT_BANK);
			}
			if(pd.getString("ACCOUNT_BANK_NUMBER")!=null && pd.getString("ACCOUNT_BANK_NUMBER")!=""){
				String ACCOUNT_BANK_NUMBER= URLDecoder.decode(pd.getString("ACCOUNT_BANK_NUMBER"), "utf-8");
				pd.put("ACCOUNT_BANK_NUMBER", ACCOUNT_BANK_NUMBER);
			}
			if(pd.getString("CURRENCY")!=null && pd.getString("CURRENCY")!=""){
				String CURRENCY= URLDecoder.decode(pd.getString("CURRENCY"), "utf-8");
				pd.put("CURRENCY", CURRENCY);
			}
			List<PageData> list = paymentUnitService.findPaymentUnitInfo(pd);
			List<PageData> list1 = new ArrayList<>();
			for(PageData p : list){				
				 p.put("ID", p.get("ID"));
				 p.put("SUPPLIER", p.getString("SUPPLIER"));
				 p.put("ACCOUNT_BANK", p.getString("ACCOUNT_BANK"));
				 p.put("ACCOUNT_BANK_NUMBER", p.get("ACCOUNT_BANK_NUMBER"));
				 p.put("CURRENCY", p.get("CURRENCY"));
				 

		            list1.add(p);
			}
		  	return ResponseResult.success(new PageInfo(list));
			/*response.setContentType("text/html; charset=utf-8");
			response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root"+"\""+":"+json.toString()+"}");				
			response.getWriter().flush();*/
	}
	/**
	 * 保存或修改支付单位信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveOrUpdatePaymentUnitInfo")
	@ResponseBody
	public String saveOrUpdatePaymentUnitInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("ID")==null || pd.getString("ID")==""){
			paymentUnitService.savePaymentUnitInfo(pd);
		}else{
			paymentUnitService.updatePaymentUnitInfo(pd);
		}
		
		return null;
	}
	/**
	 * 删除支付单位信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deletePaymentUnitInfo")
	@ResponseBody
	public String deletePaymentUnitInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();

		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			paymentUnitService.deletePaymentUnitInfo(pdq);
		}
		return null;
		
	}
	@Override
	public List getData(String type, Map param, Page page) {
		Map map = Maps.newHashMap();
		try {
			PageData pd = paymentUnitService.findPaymentUnitById(param.get("ID").toString());
			if(pd.getString("SUPPLIER")!=null && pd.getString("SUPPLIER")!=""){
				map.put("SUPPLIER", pd.getString("SUPPLIER"));
			}
			if(pd.getString("ACCOUNT_BANK")!=null && pd.getString("ACCOUNT_BANK")!=""){
				map.put("ACCOUNT_BANK", pd.getString("ACCOUNT_BANK"));
			}
			if(pd.getString("ACCOUNT_BANK_NUMBER")!=null && pd.getString("ACCOUNT_BANK_NUMBER")!=""){
				map.put("ACCOUNT_BANK_NUMBER", pd.getString("ACCOUNT_BANK_NUMBER"));
			}
			if(pd.getString("CURRENCY")!=null && pd.getString("CURRENCY")!=""){
				map.put("CURRENCY", pd.getString("CURRENCY"));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Lists.newArrayList(map);
	}
	@Override
	public String getService() {

		return  getClass().getSimpleName();
	}
	@Override
	public Map<String, String> setNames() {
		Map m = new HashMap(3);
		//保险申报单
		m.put("paymentUnit","paymentUnit.xls");
		m.put("paymentUnitName","支付单位("+ DateUtil.getSdfTimes()+").xls");
		m.put("paymentUnitModule",getService());
		return m;
	}
/**
 * 导入
 * @param file
 * @return
 * @throws Exception
 */
	@RequestMapping(value="/paymentUnitInfo")
	@ResponseBody
	public Object paymentUnitInfo(@RequestParam("file") CommonsMultipartFile file) throws Exception {
		String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
		String fileName =  FileUpload.fileUp(file, filePath, "test");	
		List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 1, 0, 0);
		PageData pdq = new PageData();
		for(PageData pd : listPd){
			if(pd.getString("var0")!=null && pd.getString("var0")!="" && pd.getString("var2")!=null && pd.getString("var2")!=""){
				PageData pdq1 = new PageData();
				pdq1.put("SUPPLIER", pd.getString("var0"));
				pdq1.put("ACCOUNT_BANK_NUMBER", pd.getString("var2"));
				PageData pdq2 = paymentUnitService.findPaymentUnitByInfo(pdq1);
				if(pdq2!=null){
					pdq.put("SUPPLIER", pd.getString("var0"));
					pdq.put("ACCOUNT_BANK", pd.getString("var1"));
					pdq.put("ACCOUNT_BANK_NUMBER", pd.getString("var2"));
					pdq.put("CURRENCY", pd.getString("var3"));
					paymentUnitService.updateInfo(pdq);
				}else{
					pdq.put("SUPPLIER", pd.getString("var0"));
					pdq.put("ACCOUNT_BANK", pd.getString("var1"));
					pdq.put("ACCOUNT_BANK_NUMBER", pd.getString("var2"));
					pdq.put("CURRENCY", pd.getString("var3"));
					paymentUnitService.savePaymentUnitInfo(pdq);
				}
			}
			
		}
		return ResponseResult.success(listPd);
		
	}
}
